const app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: '37BBZ-IASRP-7VZDT-V6FJE-USUFE-OVFGR' // 必填
});
Page({
  data: {
    show_webview: false,
    realScrollTop: 0,//页面滚动距离
    driveHeight: 0,//屏幕高度可初始化设置
    isBottom: {
      list: 0,
      goods: 0,
      shop: 0
    },
    page: 0,
    show_adv: false,
    pageNum: 1,
    advList: [],
    toutiaoIndex: 0,
    tabs: ['便民信息', '推荐商家', '商家动态'],
    currentTab: 0,
    scrollLeft: 0,
    tabIndex: 1,
    tabIndex2: 0,
    cates: [],
    userInfo: {},
    list: [],
    searchInput: '',
    cateList: [],
    hide:0,
    navigator:{hide:0},
    topic_list: [
      // {
      //   title:"大牌抢购",
      //   remark: "01：05：07",
      //   img_url: "https://www.58zltc.com/images/dapaiqianggou.png",
      //   img2_url: "/images/icon_10.png",
      //   url: "/pages/shop/goods/goods?type=qiang&title=抢购"
      // },
      {
        title: "同城特惠",
        remark: "邀请好友一起砍",
        img_url: "https://www.58zltc.com/images/fengkuangkanjia.png",
        img2_url: "/images/icon_08.png",
        url: "/pages/shop/tccar/index"
      },
      // {
      //   title: "一起拼团",
      //   remark: "新款100款",
      //   img_url: "https://www.58zltc.com/images/yiqipintuan.png",
      //   img2_url: "/images/icon_11.png",
      //   url: "/pages/shop/goods/goods?type=pin&title=拼团"
      // },
      // {
      //   title: "直播特卖",
      //   remark: "好东西都在这儿",
      //   img_url: "https://www.58zltc.com/images/zhibotemai.png",
      //   img2_url: "/images/icon_09.png",
      //   url: "/pages/quan/video/list?idx=1"
      // }
    ],
    video_list: [
      {
        title: "大牌抢购",
        remark: "01：05：07",
        img_url: "http://p1.meituan.net/mmc/08615b8ae15d03c44cc5eb9bda381cb212714.png",
        img2_url: "/images/icon_10.png",
        url: "/pages/quan/video/list?idx=1"
      },
      {
        title: "疯狂砍价",
        remark: "邀请好友一起砍",
        img_url: "http://p1.meituan.net/100.0/deal/50912a5db87cb3c40cac5ddf8723ff7c25718.jpg@90_0_279_279a%7C267h_267w_2e_90q",
        img2_url: "/images/icon_08.png",
        url: "/pages/quan/video/list?idx=1"
      },
      {
        title: "一起拼团",
        remark: "新款100款",
        img_url: "http://p1.meituan.net/mmc/08615b8ae15d03c44cc5eb9bda381cb212714.png",
        img2_url: "/images/icon_11.png",
        url: "/pages/quan/video/list?idx=1"
      }
    ]
  },
  //快捷导航
  navigator:function(e){
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1){
      var offset ='0'
      var mum = 0;
    }else{
      var offset = width*0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator:v
    })
  },
  chat: function (e) {
    //同城卡
    app.ihttpGet('https://www.58zltc.com/api.php/Shop/haveTCCard/openid/' + app.globalData.openid, function (res) {
      if (res.data.have_car == 0) {
        wx.showModal({
          title: '提示',
          content: '还没有开通同城卡，是否开通？',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/shop/tccar/tccar',
              })
            }
          }
        })
      } else {
        wx.navigateTo({
          url: '/pages/quan/member/chat',
        })
      }
    });
  },
  onPullDownRefresh: function () {
    var t = this;
    this.setData({
      page: t.data.page + 1,
      pageNum: t.data.pageNum + 1
    })
    this.onShow();
    setTimeout(function () {
      wx.stopPullDownRefresh()
      wx.showToast({
        title: '已刷新',
      })
    }, 1000)
  },
  onLoad: function (opt) {
    //识别二维码进入
    var scene = decodeURIComponent(opt.scene)
    this.setData({
      opt: opt,
      scene: scene
    })
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    var movies = [
      { url: '/images/banner_1.jpg' },
      { url: '/images/banner_2.jpg' },
      { url: '/images/banner_3.jpg' }
    ];
    this.setData({
      movies: movies
    })
    
    // app.ihttpGet("https://www.58zltc.com/api.php/Index/pian", function (res) {
    //   if (res.data.pian == 1) {
    //     that.setData({
    //       pian: 1
    //     })
    //   }
    // })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator:v
        })
      },
    })

    wx.showLoading({
      title: '加载中…',
    })
    app.ihttpGet("https://www.58zltc.com/api.php/Index/cateList", function (res) {
      that.setData({
        cateList: res.data
      })
    })
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    var post_data = { openid: app.globalData.openid };
    wx.request({
      url: "https://www.58zltc.com/api.php/Quan/unReadNum?token=" + that.data.token,
      method: 'POST',
      data: post_data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        that.setData({
          count: res.data
        })
      }
    })
    wx.getLocation({
      success: function (res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          complete: function (res) {
            var city = res.result.address_component.district;
            wx.request({
              url: 'https://www.58zltc.com/api.php/Index/weather?city=' + city + '&token=' + that.data.token,
              success: res => {
                //res.data.type = 'sunny';
                that.setData({
                  weather: res.data
                })
                switch (res.data.type) {
                  case 'duoyun':
                    console.log(res.data.type);
                    that.duoyun();
                    break;
                  case 'wind':
                    that.weifeng();
                    break;
                  case 'snow':
                    that.xiaxue();
                    break;
                  case 'sunny':
                    that.tianqing();
                    break;
                  case 'cloudy':
                    that.yintian();
                    break;
                  case 'rain':

                    that.xiayu();
                    break;
                }
                //水波
                that.shuibo();
              }
            })
          }
        });
      },
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/getAdvShop?token=' + this.data.token + '&pos=1',
      success: res => {
        if (res.data) {
          this.setData({
            shop_advInfo: res.data,
            show_adv: true
          })
        }
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/adv?type=index&token=' + this.data.token,
      success: res => {
        this.setData({
          advList: res.data
        })
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/getFlash?token=' + this.data.token,
      success: res => {
        this.setData({
          flashList: res.data
        })
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Goods/index?token=' + this.data.token,
      success: res => {
        this.setData({
          goodsList: res.data
        })
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/cate?type=index&token=' + this.data.token,
      success: res => {
        const cates = res.data;
        var news = [];
        var item = [];
        cates.forEach(function (cate, index) {
          item.push(cate);
          if (index + 1 === cates.length) {
            news.push(item);
          }
          if (item.length == 10) {
            news.push(item);
            item = [];
          }
        })
        this.setData({
          cates: news
        })
        wx.hideLoading()
      }
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          width: res.screenWidth,
          height: res.screenWidth / 7.5 * 4.8
        })
      }
    })
    setInterval(function () {
      var toutiaoIndex = that.data.toutiaoIndex;
      toutiaoIndex++;

      if (toutiaoIndex == that.data.list.list.length) {
        toutiaoIndex = 0;
      }
      that.setData({
        toutiaoIndex: toutiaoIndex
      })
    }, 2000);
  },
  follow: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中…',
    })
    var data = {};
    data.openid = app.globalData.openid;
    data.follow_openid = e.currentTarget.dataset.openid;
    app.ihttpPost('https://www.58zltc.com/api.php/Quan/follow', data, function (res) {
      if (res.data.err_code === 0) {
        wx.showToast({
          title: "成功"
        })
        setTimeout(function () {
          wx.hideLoading()
          that.onShow()
        }, 1200)

      } else {
        wx.showToast({
          title: res.data.err_msg
        })
      }
    })
  },
  onShow: function (e) {
    this.setData({
      show_webview: false
    })
    var page = this.data.page;
    let that = this;
    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {
      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })
    var openid = app.globalData.openid;
    wx.getLocation({
      type: 'wgs84',
      complete: function (resq) {
        that.setData({
          laNum: resq.latitude,
          loNum: resq.longitude
        })
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: 'https://www.58zltc.com/api.php/Index?token=' + that.data.token + '&pageNum=' + that.data.pageNum + '&la=' + resq.latitude + '&lo=' + resq.longitude + '&openid=' + openid + '&page=' + page,
          success: res => {
            that.setData({
              list: res.data
            })
          },
          complete: res => {
            wx.hideLoading();
          }
        })
      }
    })
  },
  onReady: function () {
    app.confirmCommission()
    //店主与店员建立关系
    var clerk = this.data.scene
    if (clerk != 'undefined') {
      wx.showModal({
        title: '提示',
        content: '是否成为店员',
        success: function (res) {
          if (res.confirm) {
            app.ihttpGet('https://www.58zltc.com/api.php/Index/createClerk/openid/' + clerk + '/c_openid/' + app.globalData.openid, function (res) {
              setTimeout(function () {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }, 500)
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  haodian: function (e) {
    wx.switchTab({
      url: '../shop/shop',
    })
  },
  onShareAppMessage: function (e) {
    var that = this;
    return {
      title: '推荐加入 ' + that.data.list.wechat.nick_name,
      path: '/pages/index/index?fenxiang=' + app.globalData.openid + '&fWid=' + that.data.list.wechat.wid,
      success: function (res) {
        console.log(res)
      },
      fail: function (res) {
        console.log(res)
      }
    }
  },
  switchNav: function (e) {
    var cur = e.target.dataset.current;
    var cate_id = e.target.dataset.id;
    var cate_url = e.target.dataset.url;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
      setTimeout(function () {
        wx.navigateTo({
          url: cate_url,
        })
      }, 500);
    }
    // wx.request({
    //   url: 'https://www.58zltc.com/api.php/Quan/selectNews?token=' + this.data.token + "&id=" + cate_id,
    //   success: res => {
    //     this.setData({
    //       list: res.data
    //     })
    //   }
    // })
  },
  setTab: function (e) {
    //console.log(e);
    var index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex2: index
    })
  },
  searchInputBind(e) {
    this.setData({
      searchInput: e.detail.value
    })
  },
  aa() {
    var url = "/pages/index/common/search?k=" + this.data.searchInput;
    //console.log(url);
    wx.navigateTo({
      url: "/pages/index/common/search?k=" + this.data.searchInput,
    })
  },
  //上拉到底加载更多
  onReachBottom: function () {
    // wx.showLoading({
    //   title: '加载中…',
    // })
    // var newPage = this.data.pageNum + 1;
    var t = this
    t.setData({
      isBottom: 1
    })
    // this.setData({
    //   pageNum: newPage
    // })
    // wx.request({
    //   url: 'https://www.58zltc.com/api.php/Index?token=' + this.data.token + '&pageNum=' + this.data.pageNum + '&la=' + this.data.laNum + '&lo=' + this.data.loNum,
    //   success: res => {
    //     var a = t.data.isBottom;
    //     for (var key in t.data.list) {
    //       if (key != 'goods') {
    //         if (t.data.list[key].length == res.data[key].length) {
    //           a[key] = 1;
    //           t.setData({
    //             isBottom: a
    //           })
    //         } else {
    //           a[key] = 0;
    //           t.setData({
    //             isBottom: a
    //           })
    //         }
    //       }
    //     }
    //     this.setData({
    //       list: res.data
    //     })
    //     wx.hideLoading()
    //   }
    // })
    // wx.request({
    //   url: 'https://www.58zltc.com/api.php/Goods/index?token=' + this.data.token + '&pageNum=' + this.data.pageNum,
    //   success: res => {
    //     var a = t.data.isBottom;
    //     if (t.data.goodsList.length == res.data.length) {
    //       a['goods'] = 1;
    //       t.setData({
    //         isBottom: a
    //       })
    //     }
    //     this.setData({
    //       goodsList: res.data
    //     })
    //   }
    // })
  },
  closeAdv: function () {
    this.setData({
      show_adv: false
    })
  },
  // 下面为天气动画代码
  duoyun() {
    var that = this;
    var ctx = wx.createCanvasContext('myCanvas');
    //设置云朵的初始化位置
    let yunD = [{ x: 50, y: 60, w: 50, h: 35, speed: 2 }, { x: that.data.width - 90, y: 140, w: 30, h: 21, speed: 2 }]
    //待图片加载完后，将其显示在canvas上
    wx.getImageInfo({
      src: 'https://www.58zltc.com/Public/img/yun.png',
      success: function (res) {
        var set = setInterval(function () {
          ysAnimate(res.path)
        }, 50)
      }
    })
    var set = setInterval(function () {
      ysAnimate()
    }, 50)
    //云朵运动的函数
    function ysAnimate(path) {
      ctx.clearRect(0, 0, that.data.width, that.data.height);
      for (let i = 0; i < yunD.length; i++) {
        ctx.drawImage(path, yunD[i].x, yunD[i].y, yunD[i].w, yunD[i].h);
        yunD[i].x = yunD[i].x - yunD[i].speed;
        if (yunD[i].x < -50) {
          yunD[i].x = that.data.width;
        }
      }
      ctx.draw();

    }
  },
  xiayu() {
    var that = this;
    //console.log('下雨了');
    //下雨
    var rainctx = wx.createCanvasContext('myCanvas');
    function rnd(n, m) {
      return Math.floor(Math.random() * (m - n) + n);
    };
    function d2a(n) {
      return n * Math.PI / 180;
    };
    //在画布中生成雨点：
    var rainarr = [];
    var width = 20;
    for (var i = 0; i < 200; i++) {
      rainarr.push({
        "left": rnd(0, 800),
        "top": rnd(0, 600),
        "deg": rnd(-10, 10),
        "scale": rnd(2, 10)
      });
    }
    setInterval(function () {
      rainctx.clearRect(0, 0, that.data.width, that.data.height);
      rainctx.save();
      for (var i = 0; i < rainarr.length; i++) {
        var h = 0.5 * rainarr[i].scale;
        rainarr[i].left = rainarr[i].left + Math.tan(d2a(rainarr[i].deg)) * h;
        rainarr[i].top = rainarr[i].top + h;
        //若已在画面外则删除
        if (rainarr[i].left < 0 || rainarr[i].left > that.data.width || rainarr[i].top > that.data.height) {
          rainarr.splice(i--, 1);
          continue;
        }
        var width_i = rainarr[i].scale;
        rainctx.beginPath();
        rainctx.moveTo(rainarr[i].left, rainarr[i].top);
        rainctx.lineTo(rainarr[i].left, rainarr[i].top + 6);
        rainctx.lineWidth = 2;
        rainctx.setStrokeStyle('rgba(255,255,255,.3)');
        rainctx.stroke();
        rainctx.beginPath();
      }
      rainctx.draw();
      rainctx.restore();
    }, 8);

    //不断增加新的雨点
    function rainnext() {
      setTimeout(function () {
        if (rainarr.length < 200) {
          for (var i = 0; i < 20; i++) {
            rainarr.push({
              "left": rnd(0, 800),
              "top": 0,
              "deg": rnd(-10, 10),
              "scale": rnd(2, 10)
            });
          }
        }
        rainnext();
      }, Math.random() * 200 + 200);
    };
    rainnext();
  },
  weifeng() {
    var that = this;
    //风车
    var windctx = wx.createCanvasContext('myCanvas');
    var num = 0;
    //设置定时器动画
    setInterval(function () {
      windctx.clearRect(0, 0, that.data.width, that.data.height);
      windctx.setFillStyle('white');
      buttom();
      yezi();
      windctx.draw();
    }, 5);
    //风车叶子底
    function buttom() {
      windctx.beginPath();
      var liner = windctx.createLinearGradient(48, 80, 3, 80);
      liner.addColorStop(0, "white");
      liner.addColorStop(0.5, "white");
      liner.addColorStop(1, "white");
      windctx.fillStyle = liner;
      windctx.moveTo(48, 80);
      windctx.lineTo(51, 80);
      windctx.lineTo(51, 128);
      windctx.lineTo(48, 128);
      windctx.closePath();
      windctx.fill();
    }
    function yezi() {
      windctx.save();  //保存默认情况下的canvas变换状态
      windctx.beginPath();
      windctx.translate(50, 80);//扇叶的位置
      windctx.globalAlpha = 0.98;
      // 设置第一次状态下 坐标系旋转度数
      windctx.rotate((Math.PI / 180) * num);
      var liner1 = windctx.createLinearGradient(30, -12, 30, 12);
      liner1.addColorStop(0, "white");
      liner1.addColorStop(0.5, "white");
      liner1.addColorStop(1, "white");
      windctx.fillStyle = liner1;
      windctx.save();    			 //保存第一次状态  平移坐标系变换
      windctx.beginPath();
      bind();				//调用函数
      //绘制第二片叶子
      windctx.beginPath();
      windctx.rotate((Math.PI / 180) * 120);   //坐标系旋转120°
      windctx.save();						//保存旋转坐标系状态，为第三片叶子做铺垫
      bind();			//调用函数
      //绘制第三片叶子
      windctx.beginPath();
      windctx.rotate((Math.PI / 180) * 120);   //坐标系旋转120°
      windctx.save();
      bind();     //调用函数
      windctx.restore();	//回复第3次状态前（旋转坐标系）
      windctx.restore();		//回复第2次状态前（旋转坐标系）
      //绘制叶子中心圆圈
      windctx.beginPath();
      windctx.arc(0, 0, 3, 0, Math.PI * 2);
      windctx.fill();
      windctx.restore(); 			//回复第1次状态前（平移坐标系）
      num++;   //第一状态坐标系旋转度数增加
      windctx.restore();
    }
    //绘制每片叶子都重复的代码，这里做一个函数包装
    function bind() {
      windctx.moveTo(0, 0);
      windctx.quadraticCurveTo(30, -14, 34, -4);
      windctx.lineTo(30, -2);
      windctx.fill();
    }
  },
  xiaxue() {
    var that = this;
    // 飘雪
    var snowctx = wx.createCanvasContext('myCanvas');
    function rnd(n, m) {
      return Math.floor(Math.random() * (m - n) + n);
    };

    function d2a(n) {
      return n * Math.PI / 180;
    };

    //在画布中生成雪花：
    var maxW = 800;
    var maxH = 600;
    var arr = [];
    var width = 20;

    for (var i = 0; i < 50; i++) {
      arr.push({
        "left": rnd(0, 800),
        "top": rnd(0, 600),
        "deg": rnd(-10, 10),
        "scale": rnd(2, 10)
      });
    }

    setInterval(function () {
      snowctx.clearRect(0, 0, that.data.width, that.data.height);
      snowctx.save();

      for (var i = 0; i < arr.length; i++) {
        var h = 0.5 * arr[i].scale;
        arr[i].left = arr[i].left + Math.tan(d2a(arr[i].deg)) * h;
        arr[i].top = arr[i].top + h;

        //若已在画面外则删除
        if (arr[i].left < 0 || arr[i].left > that.data.width || arr[i].top > that.data.height) {
          arr.splice(i--, 1);
          continue;
        }

        var width_i = arr[i].scale;
        var grd = snowctx.createCircularGradient(arr[i].left, arr[i].top, width_i / 2);
        grd.addColorStop(0, 'rgba(255,255,255,1)')
        grd.addColorStop(1, 'rgba(255, 255, 255, .1)')
        snowctx.setFillStyle(grd)
        snowctx.beginPath();
        snowctx.arc(arr[i].left, arr[i].top, width_i, 0, 2 * Math.PI);
        snowctx.fill();

      }
      snowctx.draw();
      snowctx.restore();
    }, 140);

    //不断增加新的雪花
    function next() {
      setTimeout(function () {
        if (arr.length < 200) {
          for (var i = 0; i < 10; i++) {
            arr.push({
              "left": rnd(0, 800),
              "top": 0,
              "deg": rnd(-10, 10),
              "scale": rnd(2, 10)
            });
          }
        }
        next();
      }, Math.random() * 300 + 500);
    };

    next();
  },
  tianqing() {
    var that = this;
    // 天晴
    var sunnyctx = wx.createCanvasContext('myCanvas');
    //设置云朵的初始化位置
    let sunnyYD = [{ x: 300, y: 70, w: 88, h: 47, speed: 2 }, { x: 120, y: 74, w: 44, h: 23, speed: 2 }, { x: 20, y: 74, w: 66, h: 35, speed: 2 }]
    //待图片加载完后，将其显示在canvas上
    var set = setInterval(function () {
      sunnyAnimate()
    }, 50)
    wx.getImageInfo({
      src: 'https://www.58zltc.com/Public/img/yunNew.png',
      success: function (res) {
        var set = setInterval(function () {
          sunnyAnimate(res.path)
        }, 50)
      }
    })
    //云朵运动的函数
    function sunnyAnimate(path) {
      sunnyctx.clearRect(0, 0, that.data.width, that.data.height);
      for (let i = 0; i < sunnyYD.length; i++) {
        sunnyctx.drawImage(path, sunnyYD[i].x, sunnyYD[i].y, sunnyYD[i].w, sunnyYD[i].h);
        sunnyYD[i].x = sunnyYD[i].x + sunnyYD[i].speed;
        if (sunnyYD[i].x > that.data.width + 90) {
          sunnyYD[i].x = -60;
        }
      }
      sunnyctx.draw();
    }
  },
  yintian() {
    var that = this;
    //阴天
    var cloudyctx = wx.createCanvasContext('myCanvas');
    //设置云朵的初始化位置
    let cloudyYD = [{ x: 120, y: 120, w: 60, h: 36, speed: 2 }, { x: 90, y: 120, w: 48, h: 28, speed: 2 }, { x: 240, y: 106, w: 29, h: 17, speed: 2 }]
    //待图片加载完后，将其显示在canvas上
    var set = setInterval(function () {
      cloudyAnimate()
    }, 100)
    wx.getImageInfo({
      src: 'https://www.58zltc.com/Public/img/cloudy.png',
      success: function (res) {
        var set = setInterval(function () {
          cloudyAnimate(res.path)
        }, 50)
      }
    })
    //云朵运动的函数
    function cloudyAnimate(path) {
      cloudyctx.clearRect(0, 0, that.data.width, that.data.height);
      for (let i = 0; i < cloudyYD.length; i++) {
        cloudyctx.drawImage(path, cloudyYD[i].x, cloudyYD[i].y, cloudyYD[i].w, cloudyYD[i].h);
        cloudyYD[i].x = cloudyYD[i].x + cloudyYD[i].speed;
        if (cloudyYD[i].x > that.data.width + 60) {
          cloudyYD[i].x = -30;
        }
      }
      cloudyctx.draw();
    }
  },
  shuibo() {
    var that = this;
    console.log('shuibo')
    //阴天
    var sbctx = wx.createCanvasContext('sb');
    // 波浪大小
    var boHeight = that.data.height / 40;
    var posHeight = that.data.height / 1.05;
    var step = 0;
    var lines = ["rgba(255,255,255, 1)", "rgba(157,192,249, 0.2)", "rgba(0,168,255, 0.2)"];
    function loop() {
      sbctx.clearRect(0, 0, that.data.width, that.data.height);
      step++;
      for (var j = lines.length - 1; j >= 0; j--) {
        // sbctx.setFillStyle = lines[j];
        var angle = (step + j * 50) * Math.PI / 180;
        var deltaHeight = Math.sin(angle) * boHeight;
        var deltaHeightRight = Math.cos(angle) * boHeight;
        sbctx.beginPath();
        sbctx.moveTo(0, posHeight + deltaHeight);
        sbctx.bezierCurveTo(that.data.width / 2, posHeight + deltaHeight - boHeight, that.data.width / 2, posHeight + deltaHeightRight - boHeight, that.data.width, posHeight + deltaHeightRight);
        sbctx.lineTo(that.data.width, that.data.height);
        sbctx.lineTo(0, that.data.height);
        sbctx.lineTo(0, posHeight + deltaHeight);
        sbctx.setFillStyle(lines[j])
        sbctx.fill()
      }
      sbctx.draw();
      setTimeout(loop, 1000 / 300);
    }
    loop();
  },
  jumpWeb: function (e) {
    console.log(e.currentTarget.dataset.type)
    let that = this
    var type_num = e.currentTarget.dataset.type
    var show_web = false
    if (type_num == 2) {
      show_web = true
    }
    this.setData({
      show_webview: show_web,
      show_adv: false
    })
  },
  onTabItemTap(item) {
    if (item.index == 0) {
      this.setData({
        show_webview: false
      })
    }
  },
  jump_news: function (e) {
    var type_name = e.currentTarget.dataset.type
    var url_name = ''
    switch (type_name) {
      case 'news':
        url_name = '/pages/quan/news/news'
        break;
      case 'huo':
        url_name = '/pages/quan/member/daren'
        break;
      case 'shops':
        url_name = '/pages/shop/shop'
        break;
      case 'goods':
        url_name = '/pages/shop/shop'
        break;
      case 'title':
        url_name = '/pages/quan/topic/list'
        break;
      case 'quan':
        url_name = '/pages/quan/quan/quan'
        break;
    }
    wx.navigateTo({
      url: url_name,
    })
  }
})
